package com.zhijian.medical.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhijian.medical.dao.local.RoutinePatientDiagnoseExtendMapper;
import com.zhijian.medical.dao.local.RoutinePatientDiagnoseMapper;
import com.zhijian.medical.entity.po.RoutinePatientDiagnoseExtend;
import com.zhijian.medical.entity.vo.request.RoutineSampleSearchVo;
import com.zhijian.medical.entity.vo.response.RoutinePatientDiagnoseVo;
import com.zhijian.medical.entity.vo.response.RoutineQcSampleVo;
import com.zhijian.medical.entity.vo.response.RoutineSampleVo;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 补充报告
 */
@Service
public class RoutinePatientDiagnoseExtendService {

    @Autowired
    RoutinePatientDiagnoseExtendMapper routinePatientDiagnoseExtendMapper;

    @Autowired
    RoutineSampleService sampleService;

    @Autowired
    RoutinePatientDiagnoseMapper routinePatientDiagnoseMapper;

    public RoutinePatientDiagnoseExtend readExtendInfo(Long diagnoseId){
        QueryWrapper<RoutinePatientDiagnoseExtend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("diagnose_id", diagnoseId);
        RoutinePatientDiagnoseExtend old = routinePatientDiagnoseExtendMapper.selectOne(queryWrapper);
        if (old != null){
            RoutinePatientDiagnoseVo routinePatientDiagnoseVo = routinePatientDiagnoseMapper.selectOneById(diagnoseId);
            List<RoutineSampleVo> routineSampleVos = sampleService.selectList(RoutineSampleSearchVo.builder()
                    .conventionPathologyNumber(routinePatientDiagnoseVo.getConventionPathologyNumber()).build());
            // 病理样本信息
            List<RoutineQcSampleVo> qcSampleVos = new ArrayList<>();
            for (RoutineSampleVo sampleVo: routineSampleVos) {
                RoutineQcSampleVo qcSampleVo = new RoutineQcSampleVo();
                BeanUtils.copyProperties(sampleVo, qcSampleVo);
                qcSampleVo.setFixedTime(sampleVo.getOffBodyTime());
                qcSampleVos.add(qcSampleVo);
            }
            old.setQcSampleVos(qcSampleVos);
        }
        return old;
    }

    public RoutinePatientDiagnoseExtend auditExtendInfo(Long diagnoseId){
        QueryWrapper<RoutinePatientDiagnoseExtend> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("diagnose_id", diagnoseId);
        RoutinePatientDiagnoseExtend upd = new RoutinePatientDiagnoseExtend();
        upd.setState(1);
        routinePatientDiagnoseExtendMapper.update(upd, updateWrapper);
        RoutinePatientDiagnoseExtend old = routinePatientDiagnoseExtendMapper.selectOne(updateWrapper);
        return old;
    }

    /**
     * 补充报告处理
     * @param routinePatientDiagnoseExtend 补充报告
     * @return
     */
    public RoutinePatientDiagnoseExtend handleExtend(RoutinePatientDiagnoseExtend routinePatientDiagnoseExtend){
        AssertUtil.check(routinePatientDiagnoseExtend.getDiagnoseId() == null, "补充报告的诊断ID不能为空！");
        QueryWrapper<RoutinePatientDiagnoseExtend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("diagnose_id", routinePatientDiagnoseExtend.getDiagnoseId());
        RoutinePatientDiagnoseExtend old = routinePatientDiagnoseExtendMapper.selectOne(queryWrapper);
        Date now = new Date();
        if (old == null){
            routinePatientDiagnoseExtend.setCreateTime(now);
            routinePatientDiagnoseExtend.setUpdateTime(now);
            routinePatientDiagnoseExtendMapper.insert(routinePatientDiagnoseExtend);
        }else {
            routinePatientDiagnoseExtend.setId(old.getId());
            routinePatientDiagnoseExtend.setUpdateTime(now);
            routinePatientDiagnoseExtend.setState(0);
            routinePatientDiagnoseExtendMapper.updateById(routinePatientDiagnoseExtend);
        }
        return routinePatientDiagnoseExtend;
    }
}
